<?php
namespace app\admin\controller;

use app\admin\model\Order;
use think\facade\Db;
use think\facade\View;
class Report extends Base{
	public $begin;
	public $end;
	public function initialize(){
        parent::initialize();
        $start_time = input('start_time');
		if(input('start_time')){
           $begin = urldecode($start_time);
            $end_time = input('end_time');
           $end = urldecode($end_time);
		}else{
           $begin = date('Y-m-d', strtotime("-2 days"));//30天前
           $end = date('Y-m-d', strtotime('+1 days'));
		}
		View::assign('start_time',$begin);
		View::assign('end_time',$end);
		$this->begin = strtotime($begin);
		$this->end = strtotime($end)+86399;
	}
	
	public function index(){
		$now = strtotime(date('Y-m-d'));
		$where = [];
		$where['pay_time'] = ['egt',$now];
		$where['order_status'] = ['gt',0];
		if($this->sid){
		    $where['sid'] = $this->sid;
        }
		$today['today_amount'] = Db::name('order')->where($where)->sum('total_shop_price');//今日销售总额
        $where = [];
        $where['add_time'] = ['egt',$now];
        if($this->sid){
            $where['sid'] = $this->sid;
        }
		$today['today_order'] = Db::name('order')->where($where)->count();//今日订单数
		if ($today['today_order'] == 0) {
			$today['sign'] = round(0, 2);
		} else {
			$today['sign'] = round($today['today_amount'] / $today['today_order'], 2);
		}
		View::assign('today',$today);
        $select_year = input('select_year');

        $where = [];
        $where[] = ['add_time','between',[$this->begin,$this->end]];
        $where[] = ['order_status','>',0];
        if($this->sid){
            $where['sid'] = $this->sid;
        }
        $res = Db::name("order".$select_year)
            ->field(" COUNT(*) as tnum,sum(total_shop_price) as amount, FROM_UNIXTIME(add_time,'%Y-%m-%d') as gap ")
            ->where($where)
            ->group('gap')
            ->select();
		foreach ($res as $val){
			$arr[$val['gap']] = $val['tnum'];
			$brr[$val['gap']] = $val['amount'];
			$tnum += $val['tnum'];
			$tamount += $val['amount'];
		}

		//支付和充值的
        $pay = Db::name('payment')->where(['status'=>1,'pay_time'=>['gt',0]])->group("from_unixtime(pay_time,'%Y-%m-%d')")->column("from_unixtime(pay_time,'%Y-%m-%d'),sum(pay_price)");

        //汇款的
        if($this->sid){
            $huikuan = 0;
        }else{
            $huikuan = Db::name('huikuan')->where(['status'=>1])->group("FROM_UNIXTIME(edittime,'%Y-%m-%d')")->column("FROM_UNIXTIME(edittime,'%Y-%m-%d'),sum(money)");
        }
        //每天的奖金播出
        $jj = Db::name('money_log')->where('action','in', ['未中奖退回','互动奖','团队奖','市场推广奖','领导奖','董事分红'])->group("FROM_UNIXTIME(w_time,'%Y-%m-%d')")->column("FROM_UNIXTIME(w_time,'%Y-%m-%d'),sum(money)");

		for($i=$this->begin;$i<=$this->end;$i=$i+24*3600){
			$tmp_num = empty($arr[date('Y-m-d',$i)]) ? 0 : $arr[date('Y-m-d',$i)];
			$tmp_amount = empty($brr[date('Y-m-d',$i)]) ? 0 : $brr[date('Y-m-d',$i)];
			$tmp_sign = empty($tmp_num) ? 0 : round($tmp_amount/$tmp_num,2);						
			$order_arr[] = $tmp_num;
			$amount_arr[] = $tmp_amount;			
			$sign_arr[] = $tmp_sign;
			$date = date('Y-m-d',$i);
			$list[] = array('day'=>$date,'order_num'=>$tmp_num,'amount'=>$tmp_amount,'sign'=>$tmp_sign,'end'=>date('Y-m-d',$i+24*60*60),
                'pay'=>$pay[$date]??'0.00',
                'huikuan'=>$huikuan[$date]??'0.00',
                'jj'=>$jj[$date]??'0.00',
                //'ys'=>$pay[$date]??0+$huikuan[$date]??0-$jj[$date]??0
                'ys'=>0
                );
			$day[] = $date;
		}
		rsort($list);
		View::assign('list',$list);
		$result = array('order'=>$order_arr,'amount'=>$amount_arr,'sign'=>$sign_arr,'time'=>$day);
		View::assign('result',json_encode($result));
		return View::fetch();
	}


	public function user(){
		$today = strtotime(date('Y-m-d'));
		$month = strtotime(date('Y-m-01'));
		$user['today'] = D('users')->where("reg_time>$today")->count();//今日新增会员
		$user['month'] = D('users')->where("reg_time>$month")->count();//本月新增会员
		$user['total'] = D('users')->count();//会员总数
		$user['user_money'] = D('Wallet')->sum('money');//会员余额总额
		//$res = Db::name('order')->cache(true)->distinct(true)->field('user_id')->group('user_id')->select();
		$res = Db::name('order')->where(['order_status'=>['in',[1,2,3,4]]])->column('user_id');
		$user['hasorder'] = count(array_flip($res));
		View::assign('user',$user);
		$sql = "SELECT COUNT(*) as num,FROM_UNIXTIME(reg_time,'%Y-%m-%d') as gap from sy_users where reg_time>$this->begin and reg_time<$this->end group by gap";
		$new = DB::query($sql);//新增会员趋势
		foreach ($new as $val){
			$arr[$val['gap']] = $val['num'];
		}
		
		for($i=$this->begin;$i<=$this->end;$i=$i+24*3600){
			$brr[] = empty($arr[date('Y-m-d',$i)]) ? 0 : $arr[date('Y-m-d',$i)];
			$day[] = date('Y-m-d',$i);
		}		
		$result = array('data'=>$brr,'time'=>$day);
		View::assign('result',json_encode($result));					
		return View::fetch();
	}
}